/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bd1.obli2012.gui;

import bd1.obli2012.framework.DatabaseManager;
import bd1.obli2012.framework.ExecutionResult;
import bd1.obli2012.framework.TablaManager;
import bd1.obli2012.framework.definicion.Columna;
import bd1.obli2012.framework.definicion.Tabla;
import bd1.obli2012.gui.backend.Contexto;
import bd1.obli2012.versionado.Cambio;
import bd1.obli2012.versionado.TipoCambio;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;

/**
 *
 * @author favio.ortelli/guillermo.nasi
 */
public class DiagloAddFK extends javax.swing.JDialog {

    private final DialogFKTabla parentDialog;
    private final Tabla tabla;

    /**
     * Creates new form DiagloAddFK
     */
    public DiagloAddFK(java.awt.Frame parent, boolean modal, DialogFKTabla parentDialog, Tabla tabla) {
        super(parent, modal);
        initComponents();

        this.parentDialog = parentDialog;
        this.tabla = tabla;

        List<Columna> cols =  new ArrayList<Columna>();
        
        for (Columna columna : tabla.getAttributes()) {
            if(!tabla.isForeignKey(columna.getNombre())){
                cols.add(columna);
            }
        }
        cmbColumna.setModel(new DefaultComboBoxModel(cols.toArray()));
        List<Tabla> tablas = DatabaseManager.getInstance().getTablesForDB(tabla.getDatabase());
        tablas.remove(tabla);
        cmbTablaRef.setModel(new DefaultComboBoxModel(tablas.toArray()));
    }

    
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        cmbColumna = new javax.swing.JComboBox();
        cmbTablaRef = new javax.swing.JComboBox();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        cmbColRef = new javax.swing.JComboBox();
        jButton1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        cmbColumna.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        cmbColumna.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmbColumnaActionPerformed(evt);
            }
        });

        cmbTablaRef.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        cmbTablaRef.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmbTablaRefActionPerformed(evt);
            }
        });

        jLabel1.setText("Columna: ");

        jLabel2.setText("Tabla Ref.:");

        jLabel3.setText("Col. Ref.:");

        jButton1.setText("Aceptar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jButton1)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(jLabel2)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(cmbTablaRef, javax.swing.GroupLayout.PREFERRED_SIZE, 199, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(jLabel1)
                            .addGap(37, 37, 37)
                            .addComponent(cmbColumna, javax.swing.GroupLayout.PREFERRED_SIZE, 199, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(jLabel3)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(cmbColRef, javax.swing.GroupLayout.PREFERRED_SIZE, 199, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(6, 6, 6)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cmbColumna, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(cmbTablaRef, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cmbColRef, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton1)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void cmbColumnaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmbColumnaActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_cmbColumnaActionPerformed

    private void cmbTablaRefActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmbTablaRefActionPerformed
        Tabla t = (Tabla)cmbTablaRef.getSelectedItem();
        cmbColRef.setModel(new DefaultComboBoxModel(t.getAttributes().toArray()));
    }//GEN-LAST:event_cmbTablaRefActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        TablaManager
                tm = new TablaManager();
        Columna col = (Columna) cmbColumna.getSelectedItem();
        Tabla tRef = (Tabla)cmbTablaRef.getSelectedItem();
        Columna colRef = (Columna)cmbColRef.getSelectedItem();
        String nombreConstraint = "fk_"+tabla.getNombre()+"_"+col.getNombre()+"_"+tRef.getNombre()+"_"+colRef.getNombre();
        ExecutionResult er = tm.addFKConstraint(tabla, nombreConstraint,col.getNombre(), tRef.getNombre(), colRef.getNombre());
        if(er.success){
            this.parentDialog.construirVista();
            MainFrame.getInstance().getPanelTabla().actualizarDatos();
            
            
            
            Map<String, String> parametros = new HashMap<String, String>();
            parametros.put("NOMBRE_TABLA", tabla.getNombre());
            parametros.put("NOMBRE_CONSTRAINT", nombreConstraint);

            Cambio cambio = new Cambio(TipoCambio.TABLA_SET_FK, parametros);
            Contexto.getInstance().guardarCambio(cambio);
            this.dispose();
        } else {
            JOptionPane.showMessageDialog(this, er.errorMessage, "Error", JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_jButton1ActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox cmbColRef;
    private javax.swing.JComboBox cmbColumna;
    private javax.swing.JComboBox cmbTablaRef;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    // End of variables declaration//GEN-END:variables
}
